package cn.xzchain.concurrency.atomic;

import cn.xzchain.concurrency.annoations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author lihang
 * @date 2018/5/7.
 * @description 原子更新类中某个属性字段的值
 */
@Slf4j
@ThreadSafe
public class Atomic5 {

    private static AtomicIntegerFieldUpdater<Atomic5> updater = AtomicIntegerFieldUpdater.newUpdater(Atomic5.class,"count");

    @Getter
    public volatile int count = 100;

    public static void main(String[] args) {
        Atomic5 atomic5 = new Atomic5();
        if (updater.compareAndSet(atomic5,100,120)){
            log.info("update success 1:{}",atomic5.count);
        }
        if (updater.compareAndSet(atomic5,100,120)){
            log.info("update success 2:{}",atomic5.count);
        }else {
            log.info("update failed:{}",atomic5.count);
        }
    }
}
